layui.use(['form','jquery','table','layer'],function(){
    var form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        layer = layui.layer;

    var tabInstance;

    //渲染表格，请求所有区域数据
    tabInstance = table.render({
        elem: '#areaList',//id选择器
        url: '/tenancy-area/getAreaList', //数据接口
        page: true, //开启分页
        limit: 5, //每页显示的条数，默认5
        limits: [5, 10, 15,], //每页显示条数选择框数据
        request: {
            pageName: 'current',
            limitName: 'size'
        },
        cols: [[ //表头
            {field: 'tenancyAreaId', title: '区域ID', width: 160, sort: true, fixed: 'left', align: 'center'},
            {field: 'tenancyAreaName', title: '区域名字',width: 240, align: 'center'},
            {field: 'operation',title: '操作', width: 180,fixed:'right',toolbar:"#areaTool", align: 'center'}
            ]],
        done: function () {
            $.post('/check/check-role',function(info) {
                part = info.part;
                console.log(info);
                if(part==3||part==2){
                    $(".add_btn").hide();
                    $("[data-field='operation']").find('#edit_btn').hide();
                    $("[data-field='operation']").find('#edit_btn').attr("class","layui-btn layui-btn-disabled layui-btn-sm layui-icon layui-icon-edit");
                    $("[data-field='operation']").find('#del_btn').hide();
                    $("[data-field='operation']").find('#del_btn').attr("class","layui-btn layui-btn-disabled layui-btn-sm layui-icon layui-icon-delete");
                }
            })
        }
    });

    //区域搜索
    form.on('submit(search)',function(data){
        console.log(data);
        tabInstance.reload({
            where :data.field
        });
        return false;
    });

    //改删操作
    table.on('tool(areaList)',function(obj){
        switch (obj.event){
            case 'edit_btn':
                layer.open({
                    type: 2,
                    content: 'edit.jsp?areaId=' + obj.data.tenancyAreaId,
                    skin: 'layui-layer-molv',
                    title: "修改区域",
                    area: ["450px", "160px"],
                    btn: ['修改', '关闭'],
                    //弹出层的初始化操作
                    success: function (layero, index) {
                        //获取编辑表单
                        var areaEdit = layer.getChildFrame('body', index).find("#edit");
                        //为表单的元素赋值
                        areaEdit.find("#areaName").val(obj.data.tenancyAreaName);
                    },
                    yes:function (index, layero) {
                        //获取编辑表单
                        var areaEdit = layer.getChildFrame('body', index).find("#edit");
                        //获取元素值
                        var areaName = areaEdit.find("#areaName").val();
                        var tipText = areaEdit.find("#tipText");

                        if(areaName.length == 0){
                            tipText.text("区域名称不得为空!");
                            return false;
                        }else {
                            tipText.text("");
                            $.post('/tenancy-area/isSameName',{"areaName":areaName,"areaId":obj.data.tenancyAreaId},function (info) {
                                if(info.data){
                                    tipText.text("该区域名已存在!");
                                    return false;
                                }else {
                                    tipText.text("");
                                    $.post('/tenancy-area/updateAreaById',
                                        {"tenancyAreaId":obj.data.tenancyAreaId,"tenancyAreaName":areaName},
                                        function(info){
                                            layer.close(index);//关闭窗口
                                            layer.msg(info.msg);//弹出增加失败或者成功的信息
                                            tabInstance.reload();//重新加载表
                                        });
                                }
                            })
                        }
                    }
                });
                break;
            case 'del_btn':
                layer.confirm('你确定要删除【'+obj.data.tenancyAreaName+'】吗？',{icon:3,anim: 4,skin:'layui-layer-molv'},function(){
                    var url = '/tenancy-area/deleteAreaById/'+obj.data.tenancyAreaId;
                    $.post(url,function(data){
                        console.log(data);
                        tabInstance.reload();//重新加载数据表格
                        layer.msg(data.msg);
                    });
                });
        }
    });

    //添加
    $(".add_btn").click(function(){
        layer.open({
            type: 2,
            content: 'add.jsp',
            skin:'layui-layer-molv',
            title:"用户管理",
            area:["450px","200px"],
            btn:['确定','关闭'],
            yes: function(index, layero){
                //获取编辑表单
                var areaAdd = layer.getChildFrame('body', index).find("#add");
                //获取元素值
                var areaName = areaAdd.find("#areaName").val();
                var tipText = areaAdd.find("#tipText");

                if(areaName.length == 0){
                    tipText.text("区域名称不得为空!");
                    return false;
                }else {
                    tipText.text("");
                    $.post('/tenancy-area/isAlikeName',{"areaName":areaName},function (info) {
                        if(info.data){
                            tipText.text("该区域名已存在!");
                            return false;
                        }else {
                            tipText.text("");
                            $.post('/tenancy-area/addArea',
                                {"tenancyAreaName":areaName},
                                function(info){
                                    layer.close(index);//关闭窗口
                                    layer.msg(info.msg);//弹出增加失败或者成功的信息
                                    tabInstance.reload();//重新加载表
                                });
                        }
                    })
                }
            }
        });
    });
});